package org.qing.config;

import java.util.Date;
import java.util.List;

import org.qing.freepx.entity.IPProxy;
import org.qing.freepx.service.IpWebProxyService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class IPProxyThread extends Thread{
	
	private IpWebProxyService ipWebProxyService;
	
	public IPProxyThread(IpWebProxyService ipWebProxyService) {
		this.ipWebProxyService = ipWebProxyService; 
	}
	
	private Boolean runFlag = true;
	
	@Override
	public void run() {
		while (runFlag) {
			log.debug("run...");
			List<IPProxy> ips = ipWebProxyService.getIPProxyList();
			for(IPProxy ipp:ips) {
				ipWebProxyService.add(ipp);
			}
			List<IPProxy> iops = ipWebProxyService.getList();
			for(IPProxy iopp:iops) {
				if(new Date().getTime()-iopp.getUpdateTime().getTime() > 360000) {
					ipWebProxyService.delete(iopp.getIp());
				}
			}
			log.debug("end...");
			try {
				Thread.sleep(180000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		super.run();
	}

	public Boolean getRunFlag() {
		return runFlag;
	}

	public void setRunFlag(Boolean runFlag) {
		this.runFlag = runFlag;
	}

}
